home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / apt / progress.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  13.7 KB  |  393 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''progress reporting classes.
  5.  
  6. This module provides classes for progress reporting. They can be used with
  7. e.g., for reporting progress on the cache opening process, the cache update
  8. progress, or the package install progress.
  9. '''
  10. import errno
  11. import fcntl
  12. import os
  13. import re
  14. import select
  15. import sys
  16. import apt_pkg
  17. __all__ = ('CdromProgress', 'DpkgInstallProgress', 'DumbInstallProgress', 'FetchProgress', 'InstallProgress', 'OpProgress', 'OpTextProgress', 'TextFetchProgress')
  18.  
  19. class OpProgress(object):
  20.     '''Abstract class to implement reporting on cache opening.
  21.  
  22.     Subclass this class to implement simple Operation progress reporting.
  23.     '''
  24.     
  25.     def __init__(self):
  26.         self.op = None
  27.         self.subOp = None
  28.  
  29.     
  30.     def update(self, percent):
  31.         '''Called periodically to update the user interface.'''
  32.         pass
  33.  
  34.     
  35.     def done(self):
  36.         '''Called once an operation has been completed.'''
  37.         pass
  38.  
  39.  
  40.  
  41. class OpTextProgress(OpProgress):
  42.     '''A simple text based cache open reporting class.'''
  43.     
  44.     def __init__(self):
  45.         OpProgress.__init__(self)
  46.  
  47.     
  48.     def update(self, percent):
  49.         '''Called periodically to update the user interface.'''
  50.         sys.stdout.write('\r%s: %.2i  ' % (self.subOp, percent))
  51.         sys.stdout.flush()
  52.  
  53.     
  54.     def done(self):
  55.         '''Called once an operation has been completed.'''
  56.         sys.stdout.write('\r%s: Done\n' % self.op)
  57.  
  58.  
  59.  
  60. class FetchProgress(object):
  61.     '''Report the download/fetching progress.
  62.  
  63.     Subclass this class to implement fetch progress reporting
  64.     '''
  65.     dlDone = 0
  66.     dlQueued = 1
  67.     dlFailed = 2
  68.     dlHit = 3
  69.     dlIgnored = 4
  70.     dlStatusStr = {
  71.         dlDone: 'Done',
  72.         dlQueued: 'Queued',
  73.         dlFailed: 'Failed',
  74.         dlHit: 'Hit',
  75.         dlIgnored: 'Ignored' }
  76.     
  77.     def __init__(self):
  78.         self.eta = 0
  79.         self.percent = 0
  80.         self.currentBytes = 0
  81.         self.currentItems = 0
  82.         self.totalBytes = 0
  83.         self.totalItems = 0
  84.         self.currentCPS = 0
  85.  
  86.     
  87.     def start(self):
  88.         '''Called when the fetching starts.'''
  89.         pass
  90.  
  91.     
  92.     def stop(self):
  93.         '''Called when all files have been fetched.'''
  94.         pass
  95.  
  96.     
  97.     def updateStatus(self, uri, descr, shortDescr, status):
  98.         '''Called when the status of an item changes.
  99.  
  100.         This happens eg. when the downloads fails or is completed.
  101.         '''
  102.         pass
  103.  
  104.     
  105.     def pulse(self):
  106.         '''Called periodically to update the user interface.
  107.  
  108.         Return True to continue or False to cancel.
  109.         '''
  110.         self.percent = (self.currentBytes + self.currentItems) * 100 / float(self.totalBytes + self.totalItems)
  111.         if self.currentCPS > 0:
  112.             self.eta = (self.totalBytes - self.currentBytes) / float(self.currentCPS)
  113.         
  114.         return True
  115.  
  116.     
  117.     def mediaChange(self, medium, drive):
  118.         '''react to media change events.'''
  119.         pass
  120.  
  121.  
  122.  
  123. class TextFetchProgress(FetchProgress):
  124.     ''' Ready to use progress object for terminal windows '''
  125.     
  126.     def __init__(self):
  127.         FetchProgress.__init__(self)
  128.         self.items = { }
  129.  
  130.     
  131.     def updateStatus(self, uri, descr, shortDescr, status):
  132.         '''Called when the status of an item changes.
  133.  
  134.         This happens eg. when the downloads fails or is completed.
  135.         '''
  136.         if status != self.dlQueued:
  137.             print '\r%s %s' % (self.dlStatusStr[status], descr)
  138.         
  139.         self.items[uri] = status
  140.  
  141.     
  142.     def pulse(self):
  143.         '''Called periodically to update the user interface.
  144.  
  145.         Return True to continue or False to cancel.
  146.         '''
  147.         FetchProgress.pulse(self)
  148.         if self.currentCPS > 0:
  149.             s = '[%2.f%%] %sB/s %s' % (self.percent, apt_pkg.SizeToStr(int(self.currentCPS)), apt_pkg.TimeToStr(int(self.eta)))
  150.         else:
  151.             s = '%2.f%% [Working]' % self.percent
  152.         print '\r%s' % s,
  153.         sys.stdout.flush()
  154.         return True
  155.  
  156.     
  157.     def stop(self):
  158.         '''Called when all files have been fetched.'''
  159.         print '\rDone downloading            '
  160.  
  161.     
  162.     def mediaChange(self, medium, drive):
  163.         '''react to media change events.'''
  164.         print "Media change: please insert the disc labeled '%s' in the drive '%s' and press enter" % (medium, drive)
  165.         return raw_input() not in ('c', 'C')
  166.  
  167.  
  168.  
  169. class DumbInstallProgress(object):
  170.     '''Report the install progress.
  171.  
  172.     Subclass this class to implement install progress reporting.
  173.     '''
  174.     
  175.     def startUpdate(self):
  176.         '''Start update.'''
  177.         pass
  178.  
  179.     
  180.     def run(self, pm):
  181.         '''Start installation.'''
  182.         return pm.DoInstall()
  183.  
  184.     
  185.     def finishUpdate(self):
  186.         '''Called when update has finished.'''
  187.         pass
  188.  
  189.     
  190.     def updateInterface(self):
  191.         '''Called periodically to update the user interface'''
  192.         pass
  193.  
  194.  
  195.  
  196. class InstallProgress(DumbInstallProgress):
  197.     """An InstallProgress that is pretty useful.
  198.  
  199.     It supports the attributes 'percent' 'status' and callbacks for the dpkg
  200.     errors and conffiles and status changes.
  201.     """
  202.     
  203.     def __init__(self):
  204.         DumbInstallProgress.__init__(self)
  205.         self.selectTimeout = 0.1
  206.         (read, write) = os.pipe()
  207.         self.writefd = write
  208.         self.statusfd = os.fdopen(read, 'r')
  209.         fcntl.fcntl(self.statusfd.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
  210.         self.read = ''
  211.         self.percent = 0
  212.         self.status = ''
  213.  
  214.     
  215.     def error(self, pkg, errormsg):
  216.         '''Called when a error is detected during the install.'''
  217.         pass
  218.  
  219.     
  220.     def conffile(self, current, new):
  221.         '''Called when a conffile question from dpkg is detected.'''
  222.         pass
  223.  
  224.     
  225.     def statusChange(self, pkg, percent, status):
  226.         '''Called when the status changed.'''
  227.         pass
  228.  
  229.     
  230.     def updateInterface(self):
  231.         '''Called periodically to update the interface.'''
  232.         if self.statusfd is None:
  233.             return None
  234.         
  235.         try:
  236.             while not self.read.endswith('\n'):
  237.                 self.read += os.read(self.statusfd.fileno(), 1)
  238.                 continue
  239.                 self
  240.         except OSError:
  241.             self.statusfd is None
  242.             (errno_, errstr) = self.statusfd is None
  243.             if errno_ != errno.EAGAIN and errno_ != errno.EWOULDBLOCK:
  244.                 print errstr
  245.             
  246.         except:
  247.             errno_ != errno.EWOULDBLOCK
  248.  
  249.         if not self.read.endswith('\n'):
  250.             return None
  251.         s = self.read
  252.         
  253.         try:
  254.             (status, pkg, percent, status_str) = s.split(':', 3)
  255.         except ValueError:
  256.             self.read.endswith('\n')
  257.             self.read.endswith('\n')
  258.             self.statusfd is None
  259.             self.read = ''
  260.             return None
  261.  
  262.         if status == 'pmerror':
  263.             self.error(pkg, status_str)
  264.         elif status == 'pmconffile':
  265.             match = re.match("\\s*'(.*)'\\s*'(.*)'.*", status_str)
  266.             if match:
  267.                 self.conffile(match.group(1), match.group(2))
  268.             
  269.         elif status == 'pmstatus':
  270.             if float(percent) != self.percent or status_str != self.status:
  271.                 self.statusChange(pkg, float(percent), status_str.strip())
  272.                 self.percent = float(percent)
  273.                 self.status = status_str.strip()
  274.             
  275.         
  276.         self.read = ''
  277.  
  278.     
  279.     def fork(self):
  280.         '''Fork.'''
  281.         return os.fork()
  282.  
  283.     
  284.     def waitChild(self):
  285.         '''Wait for child progress to exit.'''
  286.         while True:
  287.             select.select([
  288.                 self.statusfd], [], [], self.selectTimeout)
  289.             self.updateInterface()
  290.             (pid, res) = os.waitpid(self.child_pid, os.WNOHANG)
  291.             if pid == self.child_pid:
  292.                 break
  293.                 continue
  294.         return res
  295.  
  296.     
  297.     def run(self, pm):
  298.         '''Start installing.'''
  299.         pid = self.fork()
  300.         if pid == 0:
  301.             res = pm.DoInstall(self.writefd)
  302.             os._exit(res)
  303.         
  304.         self.child_pid = pid
  305.         res = self.waitChild()
  306.         return os.WEXITSTATUS(res)
  307.  
  308.  
  309.  
  310. class CdromProgress(object):
  311.     '''Report the cdrom add progress.
  312.  
  313.     Subclass this class to implement cdrom add progress reporting.
  314.     '''
  315.     
  316.     def __init__(self):
  317.         pass
  318.  
  319.     
  320.     def update(self, text, step):
  321.         '''Called periodically to update the user interface.'''
  322.         pass
  323.  
  324.     
  325.     def askCdromName(self):
  326.         '''Called to ask for the name of the cdrom.'''
  327.         pass
  328.  
  329.     
  330.     def changeCdrom(self):
  331.         '''Called to ask for the cdrom to be changed.'''
  332.         pass
  333.  
  334.  
  335.  
  336. class DpkgInstallProgress(InstallProgress):
  337.     '''Progress handler for a local Debian package installation.'''
  338.     
  339.     def run(self, debfile):
  340.         '''Start installing the given Debian package.'''
  341.         self.debfile = debfile
  342.         self.debname = os.path.basename(debfile).split('_')[0]
  343.         pid = self.fork()
  344.         if pid == 0:
  345.             res = os.system('/usr/bin/dpkg --status-fd %s -i %s' % (self.writefd, self.debfile))
  346.             os._exit(os.WEXITSTATUS(res))
  347.         
  348.         self.child_pid = pid
  349.         res = self.waitChild()
  350.         return res
  351.  
  352.     
  353.     def updateInterface(self):
  354.         '''Process status messages from dpkg.'''
  355.         if self.statusfd is None:
  356.             return None
  357.         while True:
  358.             
  359.             try:
  360.                 self.read += os.read(self.statusfd.fileno(), 1)
  361.             except OSError:
  362.                 self.statusfd is None
  363.                 (errno_, errstr) = self.statusfd is None
  364.                 if errno_ != 11:
  365.                     print errstr
  366.                 
  367.                 break
  368.             except:
  369.                 self.statusfd is None
  370.  
  371.             if not self.read.endswith('\n'):
  372.                 continue
  373.             
  374.             statusl = self.read.split(':')
  375.             if len(statusl) < 3:
  376.                 print "got garbage from dpkg: '%s'" % self.read
  377.                 self.read = ''
  378.                 break
  379.             
  380.             status = statusl[2].strip()
  381.             if status == 'error':
  382.                 self.error(self.debname, status)
  383.             elif status == 'conffile-prompt':
  384.                 match = re.match("\\s*'(.*)'\\s*'(.*)'.*", statusl[3])
  385.                 if match:
  386.                     self.conffile(match.group(1), match.group(2))
  387.                 
  388.             else:
  389.                 self.status = status
  390.             self.read = ''
  391.  
  392.  
  393.